package com.kedou.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.kedou.sdk.ui.activity.UserLoginActivity;
import com.kedou.sdk.ui.assist.OnFinishActivityListener;
import com.kedou.sdk.ui.view.DialogFactory;
import com.kedou.sdk.ui.view.ExViewPager;
import com.kedou.sdk.utils.ResUtils;
import com.kedou.sdk.utils.ToolsUtils;

/**
 * Created by Administrator on 2018/3/7.
 */

public class LoginBindDialogFragment extends PhoneVerifyDialogFragment implements View.OnClickListener, TextWatcher {

    private static final String TAG = LoginBindDialogFragment.class.getSimpleName();

    private ImageButton mCloseWindowBtn;
    private TextView mTitleTextView,mSubTitleTextView;
    private ExViewPager mExViewPager;
    private EditText mBindPhoneNumberEt;
    private EditText mVerifyCodeEt;
    private Button mSendVerifyCodeBtn;
    private Button mBindPhoneBtn;


    public static LoginBindDialogFragment newInstance(Bundle bundle) {
        LoginBindDialogFragment fragment = new LoginBindDialogFragment();
        fragment.setArguments(bundle);
        return fragment;
    }




    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        checkSumbitButtonState();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    private void checkSumbitButtonState() {
        String phoneNumber = mBindPhoneNumberEt.getText().toString().trim();
        String verifyCode = mVerifyCodeEt.getText().toString().trim();

        if (TextUtils.isEmpty(phoneNumber) || TextUtils.isEmpty(verifyCode)) {
            mBindPhoneBtn.setEnabled(false);
            return;
        }

        mBindPhoneBtn.setEnabled(true);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("hnssn_activity_bind_phone"));

        mExViewPager = contentView.findViewById(getViewIdentifier("activity_bindphone_viewpager"));
        mTitleTextView = contentView.findViewById(getViewIdentifier("activity_bindphone_title_name"));
        mSubTitleTextView = contentView.findViewById(getViewIdentifier("activity_bindphone_title_subtitle"));
        mCloseWindowBtn = contentView.findViewById(getViewIdentifier("activity_bindphone_finish_btn"));
        mCloseWindowBtn.setOnClickListener(this);
        mBindPhoneNumberEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bindphone_number_et"));
        mVerifyCodeEt = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bindphone_verify_code_et"));
        mSendVerifyCodeBtn = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bindphone_send_verify_btn"));
        mBindPhoneBtn = contentView.findViewById(ResUtils.getViewIdentifier(getActivity(), "activity_bindphone_next_btn"));

        mBindPhoneNumberEt.addTextChangedListener(this);
        mVerifyCodeEt.addTextChangedListener(this);
        mSendVerifyCodeBtn.setOnClickListener(this);
        mBindPhoneBtn.setOnClickListener(this);
        mBindPhoneNumberEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
                if(keyCode ==  EditorInfo.IME_ACTION_NEXT&&getSendVerifyCodeView().isClickable()){
                    sendPhoneVerifyCode();
                }
                Log.e("OnKeyListener",keyCode+".."+(event==null));
                return false;
            }
        });

        mVerifyCodeEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE&&mBindPhoneBtn.isEnabled()){
                    doBindCellPhoneNumber();
                }
                Log.e("CodeEtOnKeyListener",actionId+".."+(event==null));
                return false;
            }
        });




        return contentView;
    }

    private void sendPhoneVerifyCode() {
        String phoneNumber = mBindPhoneNumberEt.getText().toString().trim();

        if (TextUtils.isEmpty(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_telephonenumber"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkCellphone(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_phonecurrent"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }
        if (getActivity() instanceof UserLoginActivity) {
            ((UserLoginActivity) getActivity()).setOnPhoneVerifyCodeListener(this);
        }
        requestVerifyCode(phoneNumber, "bind_mobile");
    }

    @Override
    protected Button getSendVerifyCodeView() {
        return mSendVerifyCodeBtn;
    }

    @Override
    protected EditText getEditorVerifyCodeView() {
        return mVerifyCodeEt;
    }

    @Override
    public void onClick(View view) {
        if (view == mSendVerifyCodeBtn) {
            sendPhoneVerifyCode();
        } else if (view == mBindPhoneBtn) {
            doBindCellPhoneNumber();
        }else
        if (view == mCloseWindowBtn) {
            finishActivity();

        }
    }

    private void doBindCellPhoneNumber() {
        String phoneNumber = mBindPhoneNumberEt.getText().toString().trim();
        String verifyCode = mVerifyCodeEt.getText().toString().trim();

        if (TextUtils.isEmpty(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_telephonenumber"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkCellphone(phoneNumber)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_phonecurrent"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        if (!ToolsUtils.checkNumber(verifyCode)) {
            String message = getString(ResUtils.getStringIdentifier(getActivity(), "hnssn_hint_captcha"));
            DialogFactory.createConfirmDialog(getActivity(), message).show();
            return;
        }

        OnUserLoginActionListener listener = getOnRealNameVerifyListener();
        if (listener != null) {
            listener.doBindPhone(phoneNumber, verifyCode);
        }
    }

    private void finishActivity() {
        OnUserLoginActionListener listener = getOnRealNameVerifyListener();
        if (listener != null) {
            listener.doFinishActivity(this);
        }
    }
    private OnUserLoginActionListener getOnRealNameVerifyListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnUserLoginActionListener) {
                return (OnUserLoginActionListener) activity;
            }
        }
        return null;
    }

    public interface OnUserLoginActionListener extends OnFinishActivityListener {

        void doBindPhone(String realName,String idCardNumber);
    }


}
